Completed
Push — master ( 91b2b7...b3d439 )
by Johan
01:09
created

gulp.task(ꞌwebpack-allꞌ)   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.4285
1
var gulp = require('gulp');
2
var header = require('gulp-header');
3
var clean = require('gulp-clean');
4
var gulpWebpack = require('webpack-stream');
5
var webpack = require('webpack');
6
var babel = require("gulp-babel");
7
var runSequence = require('run-sequence');
8
var fs = require('fs');
9
10
var settings = require('./settings.json');
11
var shared = require('./shared.js');
12
13
gulp.task("clean", function(){
14
	return gulp.src(["bin/", "dist/"], {read: false})
15
		.pipe(clean());
16
});
17
18
19
gulp.task("babel", function () {
20
	return babelFunc();
21
});
22
23
24
function babelFunc(){
25
	return gulp.src("src/**/*")
26
		.pipe(babel({
27
			presets: ['es2015'],
28
			plugins: [["transform-es2015-classes", {loose: true}]]
29
		}))
30
		.pipe(gulp.dest("bin/"));
31
}
32
33
34
gulp.task("webpack", ["babel"], function () {
35
	return webpackFunc();
36
});
37
38
39
function webpackFunc(){
40
	return gulp.src('bin/JsBarcode.js')
41
		.pipe(gulpWebpack(
42
			{
43
				output: {
44
					filename: 'JsBarcode.all.js'
45
				}
46
			}
47
		, webpack))
48
		.pipe(gulp.dest("dist/"));
49
}
50
51
52
gulp.task("webpack-min", ["babel"], function () {
53
	return webpackMin('all');
54
});
55
56
57
function webpackMin(name, dest){
58
	dest = dest || './';
59
	return gulp.src('bin/JsBarcode.js')
60
		.pipe(gulpWebpack(
61
			{
62
				output: {
63
					filename: shared.minifiedFilename(name)
64
				},
65
				plugins: [new webpack.optimize.UglifyJsPlugin()]
66
			}
67
		, webpack))
68
		.pipe(header(settings.banner, require(settings.baseDir + 'package.json') ))
69
		.pipe(gulp.dest("dist/" + dest));
70
}
71
72
73
gulp.task("webpack-all", function (cb) {
74
	var barcodes = require('./barcode-building.json');
75
76
	// Move the real barcodes/index.js out of the way while compiling the individual barcodes
77
	fs.renameSync("src/barcodes/index.js", "src/barcodes/index.tmp.js");
78
79
	// Take a barcode from the barcodes array, call the functions to compile that
80
	// format and have a callback when it has finished.
81
	function loopBarcode(i){
82
		if(i < barcodes.length){
83
			createBarcodeInclude(barcodes[i], function(){
84
				loopBarcode(i + 1);
85
			});
86
		}
87
		else{
88
			fs.renameSync("src/barcodes/index.tmp.js", "src/barcodes/index.js");
89
			cb(); // Done
90
		}
91
	}
92
93
	loopBarcode(0);
94
});
95
96
97
// Takes information about a barcode formatSize
98
// Modifies the barcodes/index.js file to only import the specified format
99
// and then does a recompilation and minifies everything with webpack
100
function createBarcodeInclude(barcode, callback){
101
	var toFile = "";
102
	toFile += "import {" + barcode.names + "} from '" + barcode.barcodeFile + "'";
103
	toFile += "\n";
104
	toFile += "export default {" + barcode.names + "}";
105
106
	// Write a new barcodes/index file that only includes the specified barcode
107
	fs.writeFile("src/barcodes/index.js", toFile, function(){
108
		// Remove the compiled barcodes/index file (if it exist)
109
		if(fs.existsSync("bin/barcodes/index.js")){
110
			fs.unlinkSync("bin/barcodes/index.js");
111
		}
112
		// Re-compile with babel and webpack everything
113
		babelFunc().on('end', function(){
114
			webpackMin(barcode.name, 'barcodes/').on('end', callback);
115
		});
116
	});
117
}
118
119
120
gulp.task('compress', function(cb) {
121
	runSequence(
122
		"clean",
123
		"webpack-all",
124
		"webpack",
125
		"webpack-min",
126
		cb
127
	);
128
});
129
130
gulp.task('compile', ['babel']);
131
132
gulp.task('compile-web', ['webpack']);
133